//
//  CExpandHeader.m
//  CExpandHeaderViewExample
//
//  Created by cml on 14-8-27.
//  Copyright (c) 2014年 Mei_L. All rights reserved.
//

#define CExpandContentOffset @"contentOffset"

#import "CExpandHeader.h"
#import <UIKit/UIKit.h>

#define viewwidth _expandView.frame.size.width

@implementation CExpandHeader
{
    __weak UIScrollView *_scrollView; //scrollView或者其子类
    __weak UIView *_expandView; //背景可以伸展的View
    
    CGFloat _expandHeight;
    UILabel *_label;
    UIView *_horizonview;
    UIImageView *_backImgview;
    //问答页面的按钮和文字图片
    UIButton *_askButton;
    UIImageView *_contentimg;
}

- (void)dealloc{
    if (_scrollView) {
        [_scrollView removeObserver:self forKeyPath:CExpandContentOffset];
        _scrollView = nil;
    }
    _expandView = nil;
}


+(id)expandWithScrollView:(UIScrollView *)scrollView expandView:(UIView *)expandView Andcontent:(NSString *)content
{
    CExpandHeader *expandHeader = [CExpandHeader new];
    [expandHeader expandWithScrollView:scrollView expandView:expandView Andcontent:content];
    return expandHeader;
}

+ (id)expandWithScrollView:(UIScrollView*)scrollView expandView:(UIView*)expandView
{
    CExpandHeader *expandHeader = [CExpandHeader new];
    [expandHeader expandWithScrollView:scrollView expandView:expandView];
    return expandHeader;
}

-(void)expandWithScrollView:(UIScrollView *)scrollView expandView:(UIView *)expandView
{
    _expandHeight = CGRectGetHeight(expandView.frame);
    
    _scrollView = scrollView;
    _scrollView.contentInset = UIEdgeInsetsMake(_expandHeight, 0, 0, 0);
    [_scrollView insertSubview:expandView atIndex:0];
    [_scrollView addObserver:self forKeyPath:CExpandContentOffset options:NSKeyValueObservingOptionNew context:nil];
    [_scrollView setContentOffset:CGPointMake(0, -300)];
    
    _expandView = expandView;
    
    //使View可以伸展效果  重要属性
    _expandView.contentMode= UIViewContentModeScaleAspectFill;
    _expandView.clipsToBounds = YES;
    
    [self reSizeView];

}

//首页第二个scrollview详情里的放大图片
-(void)expandWithScrollView:(UIScrollView *)scrollView expandView:(UIView *)expandView Andcontent:(NSString *)content
{
   
     [self expandWithScrollView:scrollView expandView:expandView];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(_expandView.frame.size.width/2-130, _expandView.frame.size.height-170, 0, 0)];
    //设置自动行数与字符换行，为0标示无限制
    [_label setNumberOfLines:0];
    _label.lineBreakMode = NSLineBreakByWordWrapping;//换行方式
    NSString *strTest = content;
    UIFont *font = [UIFont fontWithName:@"Arial" size:20];
    _label.font = font;
    _label.textAlignment = NSTextAlignmentCenter;
    CGSize size = CGSizeMake(260,CGFLOAT_MAX);//LableWight标签宽度，固定的
    //计算实际frame大小，并将label的frame变成实际大小
    CGSize labelsize = [strTest sizeWithFont:font constrainedToSize:size lineBreakMode:_label.lineBreakMode];
    _label.frame = CGRectMake(_expandView.frame.size.width/2-130, _expandView.frame.size.height-170,labelsize.width, labelsize.height);
    _label.text = strTest;
    _label.textColor = [UIColor whiteColor];
    [_expandView addSubview:_label];
    
    
    //横线view
    _horizonview = [[UIView alloc]initWithFrame:CGRectMake(_expandView.frame.size.width/2-50, _label.frame.origin.y+labelsize.height+10, 100, 1)];
    _horizonview.backgroundColor = [UIColor whiteColor];
    [_expandView addSubview:_horizonview];
    
    
    //背景阴影图
    _backImgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, _expandView.frame.size.height-200, _expandView.frame.size.width, _label.frame.size.height+60)];
    _backImgview.image = [UIImage imageNamed:@"mask_case"];
    [_expandView addSubview:_backImgview];
    [_expandView bringSubviewToFront:_label];
   
    
}

//问答页面的放大图片
-(void)expandWithAskScrollView:(UIScrollView *)scrollView expandView:(UIView *)expandView;
{
    _expandHeight = CGRectGetHeight(expandView.frame);
    _expandView.userInteractionEnabled = NO;
    _scrollView = scrollView;
    _scrollView.contentInset = UIEdgeInsetsMake(_expandHeight, 0, 0, 0);
    [_scrollView insertSubview:expandView atIndex:0];
    [_scrollView addObserver:self forKeyPath:CExpandContentOffset options:NSKeyValueObservingOptionNew context:nil];
    [_scrollView setContentOffset:CGPointMake(0, -200)];
    
    _expandView = expandView;
    
    //文字图片
    _contentimg = [[UIImageView alloc]initWithFrame:CGRectMake(viewwidth/2-100, _expandView.frame.size.height-150, 200, 50)];
    _contentimg.image = [UIImage imageNamed:@"bg_font.png"];
    [_expandView addSubview:_contentimg];
    [_expandView bringSubviewToFront:_label];
    
    
    //使View可以伸展效果  重要属性
    _expandView.contentMode= UIViewContentModeScaleAspectFill;
    _expandView.clipsToBounds = YES;
    
    [self reSizeView];
    
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (![keyPath isEqualToString:CExpandContentOffset]) {
        return;
    }
    [self scrollViewDidScroll:_scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if(offsetY < _expandHeight * -1) {
        CGRect currentFrame = _expandView.frame;
        currentFrame.origin.y = offsetY;
        currentFrame.size.height = -1*offsetY;
        
        //阴影上得label(第二个scrollview)
        CGRect Frame = _label.frame;
        Frame.origin.y = currentFrame.size.height-170;
        _label.frame = Frame;
        Frame.origin.y+=_label.frame.size.height +10;
        
        //横条细线的
        CGRect Frame1 = _horizonview.frame;
        Frame1.origin.y = _label.frame.origin.y + _label.frame.size.height + 10;
        //背景阴影图片(第二个scrollview)
        CGRect frame2 = _backImgview.frame;
        frame2.origin.y = _label.frame.origin.y-30;
        _backImgview.frame = frame2;
        _horizonview.frame = Frame1;
        
        //问答页面图片的frame
        frame2 = _contentimg.frame;
        frame2.origin.y = currentFrame.size.height - 150;
        _contentimg.frame = frame2;
        
        
        _expandView.frame = currentFrame;
    }
    
}

- (void)reSizeView{

    //重置_expandView位置
    [_expandView setFrame:CGRectMake(0, -1*_expandHeight, CGRectGetWidth(_expandView.frame), _expandHeight)];
    
}

@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com
